#!/usr/bin/python -O
## printconf-import - A Python Printing configuration utility
## Copyright (C) 2000 Red Hat, Inc.
## Copyright (C) 2000 Crutcher Dunnavant <crutcher@redhat.com>,

## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os
import sys
import re
import string
import Alchemist
import traceback
#import pwd

# Die if there is no printcap on the system
if not os.path.exists('/etc/printcap'):
	raise SystemExit
#
# check the uid - if not root exit
#
if ( os.geteuid() != 0 ):
	print "You need to be root to run this!!!"
	sys.exit()
#
# check the arguments and determine if we are adding or deleting a printer
#
if ( len(sys.argv) > 2 ):
	action = None
	if ( sys.argv[1] == "-d" and len(sys.argv) == 3 ):
#		print "deleting"
		action = "Delete"
	elif ( sys.argv[1] == "-a" and len(sys.argv) == 4 ):
#		print "adding"
		action = "Adding"
	else:
		usage()
		sys.exit()
else:
	usage()
	sys.exit()

printer_name = sys.argv[2]

#

# Known entries were made by printtool, Unknown entries were not.
try: # This keeps us from ever tracebacking
	
	# Read in the local.adl file to insert data into
	ctx = Alchemist.ReadContextFromFile('/etc/alchemist/namespace/printconf/local.adl')
	# Extract the print_queues list from the local context.
	print_queues = ctx.getDataByPath('/printconf/print_queues')

	if ( action == "Delete" ):
		try:
			queue = print_queues.getChildByName(printer_name)
			queue.unlink()
		except:
			print "Error deleting in printconf database"
			raise SystemExit
	elif ( action == "Adding" ):
		try:
			queue = print_queues.addChild(Alchemist.Data.ADM_TYPE_LIST, printer_name)
			queue.setAtomic(1)
		except:
			print "printer already exists"
			raise SystemExit

		alias_list = queue.addChild(Alchemist.Data.ADM_TYPE_LIST, 'alias_list')
		alias_list.setAnonymous(1)

		queue_type = queue.addChild(Alchemist.Data.ADM_TYPE_STRING, 'queue_type')
		queue_data = queue.addChild(Alchemist.Data.ADM_TYPE_LIST, 'queue_data')

		filter_type = queue.addChild(Alchemist.Data.ADM_TYPE_STRING, 'filter_type')
		filter_data = queue.addChild(Alchemist.Data.ADM_TYPE_LIST, 'filter_data')

		# Set up the alias list
#		alias_list.addChild(Alchemist.Data.ADM_TYPE_STRING, 'alias').setValue(printer_name)

		# set the queue type to LPD
		queue_type.setValue('LPD')

		# Find the remote printer
		try:
			queue_data.addChild(Alchemist.Data.ADM_TYPE_STRING, 'lpd_server').setValue(sys.argv[3])

			queue_data.addChild(Alchemist.Data.ADM_TYPE_STRING, 'lpd_queue').setValue("dssc")
		except:
			queue.unlink()
			raise SystemExit

		# We assume yes, because, hey, why not. (Might help solaris)
		queue_data.addChild(Alchemist.Data.ADM_TYPE_BOOL, 'lpd_strict_rfc1179').setValue(1)

		filter_type.setValue('MAGICFILTER')
		filter_data.addChild(Alchemist.Data.ADM_TYPE_LIST, 'flags')

		filter_data.addChild(Alchemist.Data.ADM_TYPE_STRING, 'mf_type').setValue('POSTSCRIPT')
		filter_data.addChild(Alchemist.Data.ADM_TYPE_STRING, 'page_size').setValue('Letter')

	Alchemist.WriteContextToFile('/etc/alchemist/namespace/printconf/local.adl', ctx, mode = 0600)
	
	# Backup the printcap to printcap.save
	os.system('cp -a /etc/printcap /etc/printcap.save')

	# Zap the old printcap to force a rebuild
	os.unlink('/etc/printcap')

except:
	raise SystemExit
